<template>
  <el-main class="user-userinfo" style=" overflow: auto;
    height: calc(95vh - 110px); ">
    <el-button type="primary" icon="el-icon-back"
               v-if="showButton"
               style="position: fixed ;z-index: 999;display: flex; margin-top: -35px"
               @click="$router.back(-1)">返回
    </el-button>
    <el-form :model="recordInfo" :disabled="disabled">
      <div class="row">
        <el-form-item class="col-sm-6" label="患者姓名">
          <el-input v-model="recordInfo.name"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="性别">
          <el-input v-model="recordInfo.sex"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="年龄">
          <el-input v-model="recordInfo.age"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="婚姻状况">
          <el-input v-model="recordInfo.maritalStatus"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="民族">
          <el-input v-model="recordInfo.nationality"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="籍贯">
          <el-input v-model="recordInfo.hometown"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="出生地">
          <el-input v-model="recordInfo.birthplace"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="职业">
          <el-input v-model="recordInfo.profession"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="工作单位">
          <el-input v-model="recordInfo.employer"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="现住址">
          <el-input v-model="recordInfo.currentAddress"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="联系方式">
          <el-input v-model="recordInfo.phone"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="入院时间">
          <el-input v-model="recordInfo.admissionTime"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="记录时间">
          <el-input v-model="recordInfo.recordTime"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="病史叙述人">
          <el-input v-model="recordInfo.historyNarrator"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="可靠程度">
          <el-input v-model="recordInfo.reliability"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="病史主诉">
          <el-input type="textarea" v-model="recordInfo.mainSuit"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="现病史">
          <el-input type="textarea" v-model="recordInfo.hpi"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="既往史">
          <el-input type="textarea" v-model="recordInfo.pastHistory"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="系统回顾">
          <el-input type="textarea" v-model="recordInfo.systemReview"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="个人史">
          <el-input type="textarea" v-model="recordInfo.personalHistory"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="婚姻史">
          <el-input type="textarea" v-model="recordInfo.marriageHistory"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="生育和月经史">
          <el-input type="textarea" v-model="recordInfo.menstruationAndFertilityHistory"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="家族史">
          <el-input type="textarea" v-model="recordInfo.familyHistory"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="体格检查">
          <el-input type="textarea" v-model="recordInfo.physicalCheck"></el-input>
        </el-form-item>
      </div>
      <div class="row">
        <el-form-item class="col-sm-6" label="辅助检查">
          <el-input type="textarea" v-model="recordInfo.auxiliaryExamination"></el-input>
        </el-form-item>
        <el-form-item class="col-sm-6" label="摘要">
          <el-input type="textarea" v-model="recordInfo.summary"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <div class="edit-button" v-if="showButton">
      <el-button type="primary" @click.stop="editUserInfo">{{ btnTitle }}</el-button>
      <el-button v-show="!disabled" @click.stop="cancelEdit">取消</el-button>
    </div>
  </el-main>
</template>
<script>
import api from "../../apis/api";
export default {
  data() {
    return {
      disabled: true,
      medicalId: null
    }
  },
  computed: {
    recordInfo() {
      return this.$store.state.recordModules
    },
    btnTitle() {
      return this.disabled ? "编辑" : "保存"
    },
    showButton() {
      return this.$store.state.identity > 1
    }
  },
  methods: {

    cancelEdit() {
      this.disabled = true
    },

    async editUserInfo() {
      if (!this.disabled) {
        const edit = this.$route.params.edit
        if(edit === 1){
          const id = this.medicalId
          const recordInfo = this.$util.jsonToParse(this.recordInfo)
          recordInfo.sex = recordInfo.sex === "女" ? 0 : "1"
          const result = await api.reviseRecordInfo(recordInfo,id)
          if (result.code === 200) {
            this.$util.successMessage("修改成功")
            await this.$store.dispatch("setRecordInfo",id)
          }
        } else {
        const recordInfo = this.$util.jsonToParse(this.recordInfo)
        recordInfo.sex = recordInfo.sex === "女" ? 0 : "1"
        const result = await api.addRecordInfo(recordInfo)
        if (result.code === 200) {
          this.$util.successMessage("新增成功")
          await this.$store.dispatch("addRecordInfo")
            }else {
          this.$util.errorMessage(result.msg)
          return null
          }
        }
      }this.disabled = !this.disabled
    }
  },
  mounted() {
    console.log(this.recordInfo)
    const { medicalRecordsId,edit } = this.$route.params
    if (medicalRecordsId){
      this.medicalId = medicalRecordsId
      this.$store.dispatch("setRecordInfo", medicalRecordsId)
    }
    if (edit){
      this.disabled = false
    }
  }
}
</script>

